package com.effectivsocial.core.jsf.extension;

import java.net.URL;

import javax.faces.view.facelets.ResourceResolver;

import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.faces.facelets.impl.DefaultResourceResolver;

@Slf4j
public class SimpleResourceResolver extends DefaultResourceResolver{
	private static final Logger logger = LoggerFactory.getLogger(SimpleResourceResolver.class);
	
	private ResourceResolver parent;

    public SimpleResourceResolver(ResourceResolver parent) {
        this.parent = parent;
    }

    @Override
    public URL resolveUrl(String path) {
    	//logger.info("path ==> " + path);
        URL url = null;
        try {
        	
            url = Thread.currentThread().getContextClassLoader().getResource(path.substring(1));
            //logger.info("url ==> " + url);
            //retry first
            
            if (url == null) {
            	String alternatePath = "META-INF" + path; 
            	// logger.info("alternatePath ==> " + url);
            	url = Thread.currentThread().getContextClassLoader().getResource(alternatePath);
            	//logger.info("alternate path try url ==> " + url);
            }
            
            if (url == null) {
            	//logger.info("resolving with parent");
                url = parent.resolveUrl(path);
                
            }
            log.info("after resolving with parent ==> " + url);
            
            //u can look into it in 
            
        } catch (Exception e) {
        	logger.error("Error loading xhtml view from classpath",e);
        }
        return url;
    }
}
